<?php

class Region extends Wp_region_model {

    function __construct() {
        parent::__construct();
    }

    function getList($filter = array()) {
        $region = new Region();
        $region->addJoin(new Country());
        if(isset($filter['name']) && $filter['name']) {
            $region->addWhere("name LIKE '%".$filter['name']."%'");
        }
        if(isset($filter['country']) && $filter['country']) {
            $region->addWhere("wp_region.id_wp_country = ".intval($filter['country']));
        }
        $region->addSelect();
        $region->addSelect("wp_region.*, wp_country.name as country_name");
        $region->orderBy("name");
        $region->find();

        return $region;
    }
    
    function validateInput() {

        if (!$this->id_wp_country) {
            MessageHandler::add (_MSG_COUNTRY_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        } else {
            $country = new Country();
            if (!$country->get($this->id_wp_country)) {
                 MessageHandler::add (_MSG_COUNTRY_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
            }
        }
        
        if (empty ($this->name)) {
            MessageHandler::add (_MSG_NAME_NOT_EMPTY, MSG_ERROR, LOG_ONLY);
        }
        
        if (strlen($this->name) > MAX_LENGTH_NAME) {
            MessageHandler::add (_MSG_GROUP_NAME_IS_TOO_LONG, MSG_ERROR, LOG_ONLY);
        }
        
        if (self::isExistsByName($this->name, $this->id)) {
            MessageHandler::add (_MSG_GROUP_NAME_EXISTS, MSG_ERROR, LOG_ONLY);
        }
     
        return MessageHandler::countError() > 0 ? false : true;

    }
        
    public function isExistsByName($name = '', $id = 0, $id_wp_country = 0)
	{
		if ($name == '')
		{
			return false;
		}	
		$region = new Region();
		
        if ($id) {
			$region->addWhere("id <> {$id}");
		}
                
        $region->addWhere("name = '$name'");
        $region->addWhere("id_wp_country = '$id_wp_country'");
		$region->find();
		return ($region->countRows() > 0)?'1':'0';
	}
    
    function isDeleteAble() {
        $port = new Port();
        $port->addWhere("id_wp_region = ".$this->id);
        return ($port->countRows()>0)?false:true;
    }
    
    public function insert() {
        $this->created_date = date('Y-m-d H:i:s');
        parent::insert();
    }
    
    public function update() {
        $this->modified_date = date('Y-m-d H:i:s');
        parent::update();
    }
    
    public function getRegionByCountry($id_country = 0) {
        $region = new Region();
        if($id_country) {
            $region->addWhere("wp_region.id_wp_country = ".$id_country);
        }
        $region->addSelect();
        $region->addSelect("wp_region.*");
        $region->orderBy("name");
        $region->find();
        
        $str = '<select style="width: 180px;" name="region" id="region"><option value="">'._LB_CHOOSE.'</option>';
                                                                   
                                                            
        while ($region->fetchNext()) {
            $str .='<option value="'.$region->id.'">'.$region->name.'</option>';
        }
        $str .='</select>';
        return $str;
    }
}
